<?php
/* Description: This file is used to add sub sub category. */
#########IncludeFiles####################################################################################
include_once '../inc/config.php';
include_once("../inc/bootstrape.php");
require_once _SITE_PATH_.'classes/Vendor/Vendor.php';
require_once _SITE_PATH_.'classes/Master/Master.php';
require_once _SITE_PATH_.'classes/Product/Product.php';
require_once _SITE_PATH_.'classes/Content/Content.php';
require_once _SITE_PATH_.'classes/CommonUtility/CommonUtility.php';

$id = trim($_REQUEST['id']);
if(empty($id)){
    header("Location: "._SITE_ADMIN_URL_."deals/deal_mng.php");
    exit;
}
$result = CommonUtility::getDataById($id, 'deals');    

if( $_POST['submit']=='Submit'){   
    	$parms['name'] = $_POST['name'];
        $parms['entity_type'] = $_POST['entity_type'];
        
        if($parms['entity_type'] == 1){
            $parms['entity_id'] = $_POST['service'];
        }else if($parms['entity_type'] == 2){
            $parms['entity_id'] = $_POST['product'];
        }

        $parms['discount'] = $_POST['discount'];
        $parms['discount_type'] = $_POST['discount_type'];
        $parms['start_date'] = $_POST['start_date'];
        $parms['end_date'] = $_POST['end_date'];
        $parms['added'] = date("Y-m-d");
         
        // upload and create thumb of the image //
        if( !empty( $_FILES['deal_image'] ) ){
            $filename  = 'deal_image';     //  this is name of the control you are using in html form         
            $module='deals';
            $uploadedfile = CommonUtility::uploadNcreateThumb($filename, $module, false);
            if( $uploadedfile!= -1 )    {
                $parms['banner'] = $uploadedfile;
            }        
        }
        $condition['id'] = $id;                
        if( CommonUtility::updateData($parms, 'deals',$condition)) {        
            header("Location: deal_mng.php?msg=1166");            
            exit;
        }
    $err = 1167;    
}

$errMsg = $err;

if( $errMsg ){
    $result = $_POST;
}

$sid = '';
$pid = '';
$service_display = 'none';
$product_display = 'none';

if($result['entity_type'] == 1){
	$sid = $result['entity_id'];
	$service_display = 'block';
}else if($result['entity_type'] == 2){
	$pid = $result['entity_id'];
	$product_display = 'block';	
}

$services = array();
$services = Master::getActiveServices($sid);

$products = array();
$products = Product::getActiveProducts($pid);

$_tplName = 'deals/deal_edit.tpl';
$_title = 'Edit Deal';

require_once _ADMIN_TPL_MAIN_;
?>